use axum::{Extension, Json, extract::State, http::StatusCode, response::IntoResponse};
use chat_core::User;
use tracing::info;
use crate::{AppError, AppState, CreateChat};



pub(crate) async fn list_chat_handler(
    Extension(user): Extension<User>,
    State(state): State<AppState>,
) -> Result<impl IntoResponse, AppError> {
    info!("user ws_id: {}", user.ws_id);
    let chat = state.fetch_chats(user.id as _, user.ws_id as _).await?;
    Ok((StatusCode::OK, Json(chat)))
}

pub(crate) async fn create_chat_handler(
    Extension(user): Extension<User>,
    State(state): State<AppState>,
    Json(input): Json<CreateChat>,
) -> Result<impl IntoResponse, AppError> {
    let chat = state.create_chat(input, user.id as _, user.ws_id as _).await?;
    Ok((StatusCode::CREATED, Json(chat)))
}